#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/12/21 10:47
# @Author  : Aries
# @Site    : 
# @File    : shape_tools.py.py
# @Software: PyCharm Community Edition

import ogr
from image_tools import *
from shapely.wkb import loads

def shp2points(shppath, imgDir):
     """
     从shape线得到点
     :param shppath:
     :return:
     """
     from shapely.geometry import Point, LineString
     #shppath = LineString([Point(points['p1'][0], points['p1'][1]), Point(points['p2'][0], points['p2'][1]),
      #                     Point(points['p3'][0], points['p3'][1])]).wkt
     # creation of an empty ogr linestring to handle all possible segments of a line with  Union (combining the segements)
     profilogr = ogr.Geometry(ogr.wkbLineString)
     # open the profile shapefile
     source = ogr.Open(shppath)
     cshp = source.GetLayer()
     # union the segments of the line
     for element in cshp:
          geom = element.GetGeometryRef()
          profilogr = profilogr.Union(geom)
     # transformation in Shapely geometry
     profilshp = loads(profilogr.ExportToWkb())
     # creation the equidistant points on the line with a step of 20m
     lenght = profilshp.length
     #print lenght
     x = []
     y = []
     z = []
     # distance of the topographic profile
     dista = []
     for currentdistance  in range(0,int(lenght),20):
          # creation of the point on the line
          point = profilshp.interpolate(currentdistance)
          xp,yp=point.x, point.y
          x.append(xp)
          y.append(yp)
          # extraction of the elevation value from the MNT
          z.append(Val_raster(xp,yp,imgDir)[0])
          dista.append(currentdistance)
     return z